package com.itheima.reggie.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.domain.Category;

import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.dto.PageDto;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;

import com.itheima.reggie.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Category的业务实现类
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    //分类
    @Autowired
    private CategoryMapper categoryMapper;
    //菜品
    @Autowired
    private DishMapper dishMapper;
    //套餐
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加菜品分类
     *
     * @param category
     * @return
     */
    @Override
    public R<String> add(Category category) {
        //1.校验参数
        if (category == null) {
            return R.error("参数异常");
        }
        //判断name是否存在
        Category one = this.getOne(Wrappers.lambdaQuery(Category.class).eq(Category::getName, category.getName()));
        if (ObjectUtil.isNotNull(one)) return R.error(category.getType() == 1 ? "该菜品分类已存在" : "该套餐已存在");

        //2.补全参数
        categoryMapper.insert(category);
        return R.success("添加成功");
    }

    /**
     * 分页查询菜品分类
     *
     * @param dto
     * @return
     */
    @Override
    public R pageInfo(PageDto dto) {
        //1.校验参数
        if (dto == null) {
            return R.error("参数异常");
        }
        //查询功能
        IPage<Category> pageInfo = new Page<>(dto.getPage(), dto.getPageSize());

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        IPage<Category> page = categoryMapper.selectPage(pageInfo, wrapper);

        return R.success(page);
    }

    /**
     * 根据id删除分类，删除之前需要进行判断
     *
     * @param id
     */
    @Override
    public R removeById(Long id) {
        //判断id是否为空
        if (!ObjectUtil.isNotNull(id)) return R.error("参数不能为空");

        //1.判断这个分类下是否有菜品(查询菜品的category 如果存在这个ID的数据那么不能删除)
        LambdaQueryWrapper<Dish> dishwrapper = new LambdaQueryWrapper<>();
        dishwrapper.eq(Dish::getCategoryId, id);
        Integer dishCount = dishMapper.selectCount(dishwrapper);

        if (dishCount > 0) {
            return R.error("此分类中包含菜品，不能删除");
        }

        //2.判断这个分类下是否有套餐
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getCategoryId, id);
        Integer setmealCount = setmealMapper.selectCount(setmealWrapper);

        if (setmealCount > 0) {
            return R.error("此分类中包含套餐，无法删除");
        }

        //3.如果都没有即可删除分类
        categoryMapper.deleteById(id);
        return R.success("删除成功");
    }



}
